/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.social;

import com.nc.kyiv.yellow.sociallibrary.POJO.LibUser;
import com.nc.kyiv.yellow.sociallibrary.POJO.SessionInfo;
import com.nc.kyiv.yellow.sociallibrary.dao.UserDAO;
import com.nc.kyiv.yellow.sociallibrary.helper.Sessions;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.ldap.HasControls;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author My
 */
public class Vk extends OAuthClient{
    final String CLIENT_ID = "4039026";
    final String CLIENT_SECRET = "wotjPCDTdKMI0TyJr5Fa";
    final String REDIRECT_URI = "http://localhost:8080Social_Library/Vk";
    final String MAIN_PAGE_URL = "http://localhost:8080/Social_Library";
    final String URL_BEGINNING_FOR_ACCESS_TOKEN = "https://oauth.vk.com/access_token";
    final String URL_BEGINNIG_FOR_PERSONAL_DATA = "https://api.vk.com/method/users.get?";
    final String SCOPE = "fields=sex,bdate&access_token=";
    final String BIRTHDAY_DATE_FORMAT = "dd.MM.yyyy";
//    //final String[] keys = {"uid", "first_name", "last_name", "sex", "bdate"};
//    final Map<String, String> keysMap = new HashMap<String, String>();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String personalData = getPersonalData(req, URL_BEGINNING_FOR_ACCESS_TOKEN, CLIENT_ID,
                CLIENT_SECRET, REDIRECT_URI, URL_BEGINNIG_FOR_PERSONAL_DATA + SCOPE);
        System.out.println(personalData);

        Map<String, String> personalDataMap = parseJSON(personalData);
                        
        
        LibUser user = new LibUser("111", personalDataMap.get("uid"), personalDataMap.get("first_name"),
                personalDataMap.get("last_name"), personalDataMap.get("uid")+"vk@mail.com");
        
        if (personalDataMap.get("sex") != null) {
            user.setGender(Integer.parseInt(personalDataMap.get("sex")));
        }
        if (personalDataMap.get("bdate") != null) {
            Date birthday = getUnifiedBirthDate(personalDataMap.get("bdate"), BIRTHDAY_DATE_FORMAT);
            user.setBirthDate(birthday);
        }

        save(user);  
        login(user, req);
        resp.sendRedirect(MAIN_PAGE_URL);

    }

    @Override
    protected String getPureAccessToken(String urlForAccessToken) {
        String rawAccessToken;
        try {
            rawAccessToken = getStringFromHTMLwithGetMethod(urlForAccessToken);
            return (String) parseJSON(rawAccessToken).get("access_token");
        } catch (IOException ex) {
            System.out.println("Cant obtain vk access token");
            return null;
        }
    }
}

    

 
